package com.sunny.shop.api.v1;

import com.sunny.shop.dto.TokenDTO;
import com.sunny.shop.dto.TokenGetDTO;
import com.sunny.shop.exception.httpe.NotFoundException;
import com.sunny.shop.service.WxAuthenticationService;
import com.sunny.shop.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RequestMapping(value = "/token")
@RestController
public class TokenController {
    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    /**
     * code换取token
     * @param userData
     * @return
     */
    @PostMapping("")
    public Map<String,String> getToken(@RequestBody @Validated TokenGetDTO userData){
        Map<String,String> map = new HashMap<>();
        String token = null;
        switch (userData.getType()){
            case USER_WX:
                token = wxAuthenticationService.code2Session(userData.getAccount());
                break;
            case USER_Email:
                break;
            default:
                throw new NotFoundException(20001);
        }
        map.put("token", token);
        return map;
    }

    @PostMapping("/verfify")
    public Map<String, Boolean> verfify(@RequestBody TokenDTO token){
        Map<String, Boolean> map = new HashMap<>();
        Boolean verify = JwtToken.verifyToken(token.getToken());
        map.put("is_verify", verify);
        return map;
    }
}
